<template>
  <a-card>
    <a-form-model
      :form="form"
      ref="ruleForm"
      class="form">
      <a-form-model-item
        prop="title"
      >
        <a-input
          placeholder="请输入文章标题"
          v-model="form.title"
        />
      </a-form-model-item>
      <a-form-model-item
        prop="content"
      >
        <rich-text
          style="height: 600px;z-index:0;position: relative;"
          v-model="form.content"
        />
      </a-form-model-item>
    </a-form-model>

    <footer-tool-bar>
      <a-button
        style="margin-right: 10px"
        type="danger"
        @click="handleSaveDraft"
      >保存草稿</a-button>
      <a-button
        style="margin-right: 10px"
        type="primary"
        @click="handleShowPostSetting"
      >发布</a-button>
    </footer-tool-bar>
    <post-details-add
      ref="postDetailsAdd"
      @parentSource="getParentSource"
    />
  </a-card>
</template>

<script>
import FooterToolBar from '@/components/FooterToolbar/FooterToolBar'
import RichText from '@comp/editer/RichText'
import postDetailsAdd from './postDetailsAdd'
import { saveByPost, getObj, updateByPost } from '@/api/cms/cmsPost'
import moment from 'moment'
export default {
  name: 'PostAdd',
  components: {
    RichText,
    FooterToolBar,
    postDetailsAdd
  },
  data () {
    return {
      form: {
        id: '',
        title: '',
        content: ''
      },
      dataSource: {},
      type: '',
      treeData: [],
      status: ''
    }
  },
  methods: {
    edit (id, type) {
      if (['edit'].includes(type)) {
        getObj(id).then(res => {
          const record = res.data
          this.dataSource = record
          this.form.id = record.id
          this.form.title = record.title
          this.form.content = record.formatContent
          this.status = record.status
        })
      } else {
      }
    },
    // 保存草稿
    handleSaveDraft () {
      if (this.type === 'add') {
        if (!this.form.title) {
          this.form.title = moment(new Date()).format('YYYY-MM-DD-HH-mm-ss')
        }
        const post = {
          formatContent: this.form.content,
          title: this.form.title
        }
        // 草稿
        post.status = '0'
        saveByPost(post).then(res => {
          this.$message.success('保存到草稿箱')
          this.$router.push({ name: 'postList' })
        }).catch(err => {
          this.$message.error(err.msg)
        })
      } else if (this.type === 'edit') {
        const post = {
          id: this.form.id,
          formatContent: this.form.content,
          title: this.form.title
        }
        post.status = '0'
        updateByPost(post).then(res => {
          this.$message.success('修改成功')
          this.$router.push({ name: 'postList' })
        }).catch(err => {
          this.$message.error(err.msg)
        })
      }
    },
    // 预览
    handlePreview () {

    },
    // 发布
    handleShowPostSetting () {
      // 点击发布
      if (this.type === 'add') {
        this.$refs.postDetailsAdd.edit('', 'add')
      } else if (this.type === 'edit') {
        this.$refs.postDetailsAdd.edit(this.dataSource, 'edit')
      }
    },
    // 将父组件的数据传递给子组件
    getParentSource () {
      const post = {
        id: this.form.id,
        formatContent: this.form.content,
        title: this.form.title,
        status: this.status
      }
      if (!this.form.title) {
        post.title = moment(new Date()).format('YYYY-MM-DD-HH-mm-ss')
      }
      this.$refs.postDetailsAdd.getParentSource(post)
    }
  },
  mounted () {
    const id = this.$route.query.id
    this.type = this.$route.query.type
    this.edit(id, this.type)
  }
}
</script>

<style scoped>

</style>
